/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihomeclient;

import be.contribute.common.controllers.PeripheralController;
import be.contribute.common.pojo.Temperature;
import be.contribute.common.pojo.TextMessage;
import java.util.List;

/**
 *
 * @author debian
 */
public class PiHomeClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if(args.length == 0)
        {
            System.out.println("Please enter the WS URL");
            System.exit(1);
        }
        final int fSleepTimeInMillies = 30002;
        ServiceClient vClient = new ServiceClient(args[0]);
        PeripheralController vPeripheralController = PeripheralController.getInstance();
        vPeripheralController.initGSM();
        boolean vFirst = true;
        while(true)
        {
            try {
                if(vFirst)
                {   
                    vPeripheralController.sendText(2);
                    vFirst = false;
                }
                
                Thread.sleep(fSleepTimeInMillies);
                //List<Temperature> tempList = vPeripheralController.readOwnTemperature();
                //Temperature averageTemp = Temperature.averageFromList(tempList);
                //if(averageTemp != null)
                   // vClient.insertTemperature(averageTemp);
                List<TextMessage> vList = vPeripheralController.readTexts();
                if(vList != null)
                {
                    for(TextMessage vText: vList)
                        {
                            boolean vActionComplete = determineTextMessageAction(vText.getMessage(), vClient);
                            if(vActionComplete)
                            {
                                vPeripheralController.sendText(1);
                            }
                        } 
                }    
                
            } catch (InterruptedException ex) {
                 vPeripheralController.sendText(0);
                 ex.printStackTrace();
            }
        }
    }
    
    public static boolean determineTextMessageAction(String pInput, ServiceClient pClient)
    {
        final String HEAT_CMD = "HEAT";
        final String AIRCO_CMD = "AIRCO";
        
        if(pInput.indexOf("CTB") != -1 && pInput.length() > 4)
        {
            pInput = pInput.substring(4);
            if(pInput.indexOf(HEAT_CMD) != -1)
            {
                pInput = pInput.substring(HEAT_CMD.length() + 1);
                String[] data = pInput.split(" ");
                if(data.length >= 2)
                {
                    String roomId = data[0];
                    String state = data[1];
                    pClient.controlHeat(roomId, state);
                    return true;
                }
                return false;
            }
            
            if(pInput.indexOf(AIRCO_CMD) != -1)
            {
                pInput = pInput.substring(AIRCO_CMD.length() + 1);
                String[] data = pInput.split(" ");
                if(data.length >= 2)
                {
                    String roomId = data[0];
                    String state = data[1];
                    pClient.controlAirco(roomId, state);
                    return true;
                }
                return false;
            }
        }
        return false;
    }
}
